﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Code.Models;
using System.Security.Cryptography;

namespace QianBuoToolForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var salts = (Enum.GetValues(typeof(Salt)) as Salt[]).Select(p => new { Text = p.ToString(), Value = p }).ToList();
            cboSalt.Items.Clear();
            cboSalt.DisplayMember = "Text";
            cboSalt.ValueMember = "Value";
            salts.Add(new { Text = "自定义", Value = (Salt)(-1) });
            cboSalt.DataSource = salts;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtPass.Text))
            {
                MessageBox.Show("请输入要加密的字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if ((int)cboSalt.SelectedValue == -1 && string.IsNullOrEmpty(txtSalt1.Text) && string.IsNullOrEmpty(txtSalt2.Text))
            {
                MessageBox.Show("请输入自定义盐", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            try
            {
                if ((int)cboSalt.SelectedValue == -1)
                {
                    if (string.IsNullOrEmpty(txtSalt1.Text))
                        txtOut.Text = MD5.Create().ComputeHash(txtPass.Text, txtSalt2.Text);
                    else if (string.IsNullOrEmpty(txtSalt2.Text))
                        txtOut.Text = MD5.Create().ComputeHash(txtPass.Text, txtSalt1.Text);
                    else
                        txtOut.Text = new Code.Commen.DES().EncryptString(txtPass.Text, txtSalt1.Text, txtSalt2.Text);
                    return;
                }
                var salt = (Salt)cboSalt.SelectedValue;
                if (cboType.Text == "MD5")
                {
                    MD5 md5 = MD5.Create();
                    txtOut.Text = md5.ComputeHash(txtPass.Text, salt.GetMd5Salt());
                }
                else if (cboType.Text == "DES")
                {
                    var des = new Code.Commen.DES();
                    txtOut.Text = des.EncryptString(txtPass.Text, salt.GetDesSalt().Key, salt.GetDesSalt().Value);
                }
                else
                {
                    MessageBox.Show("请选择要加密类型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cboSalt_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((int)cboSalt.SelectedValue == -1)
            {
                cboType.Enabled = false;
                txtSalt1.Enabled = true;
                txtSalt2.Enabled = true;
            }
            else
            {
                cboType.Enabled = true;
                txtSalt1.Enabled = false;
                txtSalt2.Enabled = false;
            }
        }
    }
}
